/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tarea;

import java.util.Stack;
import pilas.pila;

/**
 *
 * @author daya
 */
public class PilasDeber extends javax.swing.JFrame {

    /**
     * Creates new form PilasDeber
     */
    pila nuevaPila;

    public PilasDeber() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtExpesion = new javax.swing.JTextField();
        txtRespuesta = new javax.swing.JTextField();
        btnPostfija = new javax.swing.JButton();
        btnPrefija = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtExpesion.setText("(6+2)*3/2^2-4");
        txtExpesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtExpesionActionPerformed(evt);
            }
        });

        txtRespuesta.setText("Respuesta");

        btnPostfija.setText("Postfija");
        btnPostfija.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPostfijaActionPerformed(evt);
            }
        });

        btnPrefija.setText("Prefija");
        btnPrefija.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrefijaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnPrefija)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnPostfija)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(txtExpesion, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(103, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtExpesion, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPostfija))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRespuesta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPrefija))
                .addContainerGap(215, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPostfijaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPostfijaActionPerformed
        // TODO add your handling code here:
        nuevaPila = new pila();
        String infija = txtExpesion.getText();
        Stack<Character> resultado = nuevaPila.getPostfija(infija);
        if (!resultado.empty()) {
            txtRespuesta.setText(nuevaPila.getDatosPila(resultado));
        } else {
            txtRespuesta.setText("Expresion no valida.");
        }
    }//GEN-LAST:event_btnPostfijaActionPerformed

    private void txtExpesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtExpesionActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtExpesionActionPerformed

    private void btnPrefijaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrefijaActionPerformed
        // TODO add your handling code here:
        nuevaPila = new pila();
        String infija = txtExpesion.getText();
        String invertirTexto = nuevaPila.getInvertirCadena(infija);
        Stack<Character> resultado = nuevaPila.getPrefija(invertirTexto);
        if (!resultado.empty()) {
            String invertirSolucion = nuevaPila.getDatosPila(resultado);
            invertirSolucion = nuevaPila.getInvertirCadena(invertirSolucion);
            txtRespuesta.setText(invertirSolucion);
        } else {
            txtRespuesta.setText("Expresion no valida.");
        }
    }//GEN-LAST:event_btnPrefijaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PilasDeber.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PilasDeber.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PilasDeber.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PilasDeber.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PilasDeber().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPostfija;
    private javax.swing.JButton btnPrefija;
    private javax.swing.JTextField txtExpesion;
    private javax.swing.JTextField txtRespuesta;
    // End of variables declaration//GEN-END:variables
}
